﻿#include "montorprocess.h"
#include <QApplication>
#include <QSettings>
#include <QFile>
#include <QDebug>


MontorProcess * MontorProcess::m_montorProcess = NULL;
MontorProcess::MontorProcess(QObject *parent) : QObject(parent)
  , m_timer(NULL)
  , m_timeLens(0)
{
    if(!getStartCompany()){
        setStartCompany(true);
    }
    QString docParhFile =  QApplication::applicationDirPath();
    QString docParhs = docParhFile;
    m_updateSourcePahts = docParhs;
    m_timer = new QTimer(this);
    connect(m_timer,SIGNAL( timeout() ) ,this ,SLOT( onTestTimeout() ) );
    if(m_timer != NULL){
        m_timer->start(5000);
    }
}

MontorProcess::~MontorProcess()
{

}

MontorProcess *MontorProcess::gestance()
{
    if(m_montorProcess == NULL){
        m_montorProcess = new  MontorProcess();
    }
    return m_montorProcess;
}

void MontorProcess::setStartCompany(bool status)
{
    /*
    QString appName = QApplication::applicationName();//程序名称

    QString appPath = QApplication::applicationFilePath();// 程序路径

    appPath = appPath.replace("/","\\");

    QSettings *reg=new QSettings(
                "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
                QSettings::NativeFormat);

    QString val = reg->value(appName).toString();// 如果此键不存在，则返回的是空字符串
    if(val != appPath) {
        if(status){
            reg->setValue(appName,appPath);// 如果移除的话，reg->remove(applicationName);
        }else {
            reg->remove(appName);
        }
    }else {
        if(!status){
            reg->remove(appName);
        }
    }

    reg->deleteLater();
*/
}

bool MontorProcess::getStartCompany()
{
    /*
    QString appName = QApplication::applicationName();//程序名称

    QString appPath = QApplication::applicationFilePath();// 程序路径

    appPath = appPath.replace("/","\\");

    QSettings *reg=new QSettings(
                "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
                QSettings::NativeFormat);

    QString val = reg->value(appName).toString();// 如果此键不存在，则返回的是空字符串
    reg->deleteLater();
    if(val != appPath) {
        return  false;
    }else {
        return  true;
    }
    */
    return true;

}

bool MontorProcess::checkAppStatus(const QString &appName)
{

    QString cmds = QString("ps -ef | grep %1").arg(appName);
    QProcess process;
    process.start(cmds.toUtf8());   //执行tasklist程序
    process.waitForFinished(5000);    //阻塞5秒等待tasklist程序执行完成，超过五秒则直接返回
    QString outputStr = QString::fromLocal8Bit(process.readAllStandardOutput()); //把tasklist程序读取到的进程信息输出到字符串中
    qDebug()<<__func__<<__LINE__<<"outputStr=="<<outputStr;
    QStringList listInfors = outputStr.split("\n");
    bool statues = false;
    process.close();
    for(int i =0 ; i < listInfors.size() ;i++){
        QString items = listInfors.at(i);
        qDebug()<<__func__<<__LINE__<<"i=="<<items<<"items=="<<items;
        if(items.contains(appName)&& !items.contains("grep "))
        {
            statues  = true;
            qDebug()<<__func__<<__LINE__<<"statues=="<<statues;

        }
    }
    return statues;
    /*
    if(outputStr.contains(appName)&& !outputStr.contains("grep "))
    {

        process.close(); //用完记得把process关闭了，否则如果重新调用这个函数可以会失败
        return true;
    }
    else
    {

        process.close();
        return false;
    }
    */

}

QString MontorProcess::readFlile()
{
    QString infors="";
    QString filea = m_updateSourcePahts+"/tag.txt";
    QFile file(filea);
    if(file.open(QIODevice::ReadOnly)){
        infors = file.readAll();
        file.close();
    }
    return infors;
}

void MontorProcess::onTestTimeout()
{
    if(m_timer != NULL){
        m_timer->stop();
    }
    if(!checkAppStatus("luckcontrol") ){
        QString infora =   readFlile();
        if(!infora.contains("1")){

            QString exes = m_updateSourcePahts+"/luckcontrol";
            QStringList lista;
            lista <<"&";
            qDebug()<<__func__<<__LINE__<<"exes=="<<exes;
            qDebug()<<__func__<<__LINE__<<"lista=="<<lista;
            QProcess::startDetached(exes,lista);
        }
    }
    /*
    if(!checkAppStatus("DebugView++.exe") ){
        m_timeLens = 0;
        QString exes = m_updateSourcePahts+"/DebugView++.exe";
        QProcess::startDetached(exes);

    }else{
        if(m_timeLens >= 1660){
            killProcess("DebugView++.exe");
            m_timeLens = 0;
            QString exes = m_updateSourcePahts+"/DebugView++.exe";
            QProcess::startDetached(exes);
        }
        m_timeLens ++;
    }
    */
    if(m_timer != NULL){
        m_timer->start(5000);
    }
}

void MontorProcess::killProcess(const QString &processName) {
    QProcess::execute("killall", QStringList() << processName );
}
